package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.ScoreGroupMemberEntity;
import com.sinothk.gpserver.modules.biz.service.IScoreGroupMemberService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  1. 积分-俱乐部用户积分
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-29
 */
@Api(tags = "积分-俱乐部用户积分")
@RequestMapping("/scoreGroupMember")
@ResponseResult()
@RestController
public class ScoreGroupMemberController extends BaseController {

    @Resource
    private IScoreGroupMemberService scoreGroupMemberService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<ScoreGroupMemberEntity> page(@RequestBody PageVo<ScoreGroupMemberEntity> pageVo) {
        return scoreGroupMemberService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public ScoreGroupMemberEntity get(@ApiParam("id") @PathVariable String id) {
        return scoreGroupMemberService.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestBody ScoreGroupMemberEntity scoreGroupMemberEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        scoreGroupMemberEntity.setCreateBy(login.getAccount());
        scoreGroupMemberService.save(scoreGroupMemberEntity);
        return "新增成功";
    }


    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public ScoreGroupMemberEntity update(@RequestBody ScoreGroupMemberEntity scoreGroupMemberEntity) {
        if (StringUtil.isEmpty(scoreGroupMemberEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        scoreGroupMemberEntity.setUpdateBy(login.getUserName());
        scoreGroupMemberService.updateById(scoreGroupMemberEntity);
        return scoreGroupMemberService.getById(scoreGroupMemberEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody ScoreGroupMemberEntity scoreGroupMemberEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(scoreGroupMemberEntity.getId())) {
            scoreGroupMemberEntity.setCreateBy(login.getAccount());
            scoreGroupMemberService.save(scoreGroupMemberEntity);
            return "新增成功";
        } else {
            scoreGroupMemberEntity.setUpdateBy(login.getUserName());
            scoreGroupMemberService.updateById(scoreGroupMemberEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        scoreGroupMemberService.removeById(id);
        return "已逻辑删除";
    }
}
